package com.pkk.components.apollo.dynamic;

import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * @description: 更新一些配置
 * @author: peikunkun
 * @create: 2019-08-31 16:45
 **/
@Configuration
@EnableApolloConfig
public class DynamicPropertiesConfig implements ApplicationContextAware {

  private ApplicationContext applicationContext;


  @ApolloConfigChangeListener
  public void change(ConfigChangeEvent configChangeEvent) {
    refreshProperties(configChangeEvent.changedKeys());
  }

  /**
   * @Description: 刷新系统的配置
   * @Param: [changedKeys]
   * @return: void
   * @Author: peikunkun
   * @Date: 2019/8/31 0031 下午 4:48
   */
  private void refreshProperties(Set<String> changedKeys) {
    this.applicationContext.publishEvent(new EnvironmentChangeEvent(changedKeys));
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
  }

}
